package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.AlbumMapper;
import cn.tedu.hotel.mapper.AlbumRoomMapper;
import cn.tedu.hotel.mapper.RoomMapper;
import cn.tedu.hotel.pojo.dto.AlbumRoomAddNewDTO;

import cn.tedu.hotel.pojo.entity.AlbumRoom;
import cn.tedu.hotel.pojo.vo.AlbumRoomListItemVO;
import cn.tedu.hotel.pojo.vo.AlbumRoomStandardVO;
import cn.tedu.hotel.pojo.vo.AlbumStandardVO;
import cn.tedu.hotel.pojo.vo.RoomStandardVO;
import cn.tedu.hotel.service.IAlbumRoomService;
import cn.tedu.hotel.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  处理相册与房间关联的业务实体类
 */
@Slf4j
@Service
public class AlbumRoomServiceImpl implements IAlbumRoomService, ServiceCode {

    @Autowired
    AlbumRoomMapper albumRoomMapper;
    @Autowired
    AlbumMapper albumMapper;
    @Autowired
    RoomMapper roomMapper;

    public AlbumRoomServiceImpl(){
        log.debug("创建业务对象：AlbumRoomServiceImpl");
    }

    @Override
    public void addNew(AlbumRoomAddNewDTO albumRoomAddNewDTO) {
        log.debug("开始处理【添加相册与房间关联数据】的业务，参数：{}",albumRoomAddNewDTO);
        // 分别获取albumId 和 roomId
        Long albumId = albumRoomAddNewDTO.getAlbumId();
        Long roomId = albumRoomAddNewDTO.getRoomId();

        //检查相册数据是否存在
        {
            AlbumStandardVO queryResult = albumMapper.getStandardById(albumId);
            if(queryResult == null){
                String message = "添加相册与房间的关联失败，相册数据不存在";
                log.debug(message);
                throw new ServiceException(ERR_NOT_FOUND,message);
            }
        }

        //检查房间数据是否存在
        {
            RoomStandardVO queryResult = roomMapper.getStandardById(roomId);
            if (queryResult == null){
                String message = "添加相册与房间的关联失败，房间数据不存在";
                log.debug(message);
                throw new ServiceException(ERR_NOT_FOUND,message);
            }
        }

        // 检查相册与房间的关联数据是否存在
        {
            int count = albumRoomMapper.countByRoomAndType(albumId, roomId);
            if (count > 0){
                String message = "添加相册与房间关联失败，此相册与房间已有关联";
                log.debug(message);
                throw new ServiceException(ERR_CONFLICT,message);
            }
        }

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //创建对象
        AlbumRoom albumRoom = new AlbumRoom();
        //补全属性值
        BeanUtils.copyProperties(albumRoomAddNewDTO,albumRoom);
        albumRoom.setGmtCreate(now);
        albumRoom.setGmtModified(now);

        //执行插入数据
        int rows = albumRoomMapper.insert(albumRoom);
        if (rows != 1){
            String message = "添加相册与房间的关联失败，服务器繁忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ERR_INSERT,message);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据roomId={}删除相册与房间关联数据】的业务",id);
        //检查要删除的数据是否存在
        AlbumRoomStandardVO queryResult = albumRoomMapper.getStandardById(id);
        if (queryResult == null){
            String message = "删除相册与房间的关联失败，关联数据不存在";
            log.debug(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        //执行删除
        int rows = albumRoomMapper.deleteById(id);
        if (rows != 1){
            String message = "删除相册与房间的关联失败，服务器繁忙，请稍后再试";
            log.debug(message);
            throw new ServiceException(ERR_INSERT,message);
        }

    }



}
